declare var $: any;
declare var template: any;
declare var toastr: any;
declare var swal: any;
declare var bootbox: any;
declare var layer: any;

class CommonPage {
    static param = {
        currentPage: 1,
        totalPage: 1,
        key: '',
        url: ''
    }
    static flag = {
        addUrl: false,
        delPar: {},
        delUrl: ''
    }

    constructor(param: { currentPage: number; totalPage: number; key: string; url: string }) {
        CommonPage.param = param;
    }

    /*初始化*/
    init() {//初始化
        $('.data-list input,.checkAll,.reverse-selection').iCheck({
            checkboxClass: 'icheckbox_flat-orange',
            radioClass: 'icheckbox_flat-orange'
        })
        //全选
        $('.checkAll').on('ifChecked', function (event) {
            $('.data-list tbody input').iCheck('check');
        });
        $('.checkAll').on('ifUnchecked', function (event) {
            $('.data-list tbody input').iCheck('uncheck');
        });
        //反选
        $('.reverse-selection').on('ifChecked ifUnchecked', function (event) {
            var uncheck = $('.data-list tbody input:checked');
            var check = $('.data-list tbody input:not(:checked)');
            uncheck.iCheck('uncheck');
            check.iCheck('check');
        })
        //数字输入限制
        $("body").delegate('.input-number', 'keyup', function () {
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        }).bind("paste", function () {  //CTR+V事件处理
            $(this).val($(this).val().replace(/\D|^0/g, ''));
        })
        //日期格式化

        /*分页初始化*/
        $('#page-navigation').bootstrapPaginator({
            bootstrapMajorVersion: 4,
            currentPage: CommonPage.param.currentPage < 1 ? 1 : CommonPage.param.currentPage,
            totalPages: CommonPage.param.totalPage < 1 ? 1 : CommonPage.param.totalPage,
            pageUrl: '#',
            itemTexts: function (type, page, current) {//设置显示的样式，默认是箭头
                switch (type) {
                    case "first":
                        return "首页";
                    case "prev":
                        return "上一页";
                    case "next":
                        return "下一页";
                    case "last":
                        return "末页";
                    case "page":
                        return page;
                }
            },
            //点击事件
            onPageClicked: function (event, originalEvent, type, page) {
                var url = CommonPage.param.url;
                var key = $('.selectpicker').selectpicker('val');
                var word = $('#curSearchInput').val();
                if (word == null || word == 'Search' || word == '' || key == null) {
                    url += "/index.do?n=" + page;
                } else {
                    url += "/search.do?n=" + page + '&key=' + key + '&word=' + word;
                }
                location.href = url;
            }
        });
        /*查询条件初始化*/
        $('.selectpicker').selectpicker({style: 'btn-sm'});
        $('.selectpicker').selectpicker('val', [CommonPage.param.key]);
    }

    /*打开 添加modal*/
    static addPage() {
        CommonPage.flag.addUrl = true;
        $('#editModallLabel').html("添加/New")
        $('#editModal-body').html('').html(template('tpl-edit', {}));
        $('#editModal').modal('show');
        this.timeInit()
    }

    /* 打开编辑modal*/
    static editPage(id) {
        CommonPage.flag.addUrl = false;
        $('#editModallLabel').html("修改/Edit")
        $('#editModal-body').html('<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>')
        $('#editModal').modal('show');
        $.post(CommonPage.param.url + "/get.do", {id: id}, function (data) {
            $('#editModal-body').html('').html(template('tpl-edit', data.data));
            CommonPage.timeInit();
        });
    }

    /*打开删除modal*/
    static delPage(id) {
        this.bootBoxDel(CommonPage.param.url + '/del.do', {id: id});
    }

    /*打开删除所有*/
    static delAllPage() {
        var par = new Array();
        var show = false;
        $('.data-list tbody input:checked').each(function () {
            show = true;
            par.push($(this).attr('data'));

        })
        if (show) {
            this.bootBoxDel(CommonPage.param.url + '/delAll.do', {ids: par});
        } else {
            layer.tips('选择删除内容!', '#check-all');
        }
    }

    /*确认编辑修改或者增加*/
    static confirmEdit() {
        $.post(CommonPage.flag.addUrl ? CommonPage.param.url + "/add.do" : CommonPage.param.url + "/merge.do", $("#edit-form").serialize(), function (data) {
            if (data.ret == 1) {
                location.reload();
            } else if (data.ret == 0) {
                $('#editTips').html(data.msg)
            }
        });
    }

    /*确认删除*/
    static confirmDel() {
        $.post(CommonPage.flag.delUrl, CommonPage.flag.delPar, function (data) {
            if (data.ret == 1) {
                location.reload();
            } else if (data.ret == 0) {
                toastr.error(data.msg)
            }
        });
    }

    /*搜索*/
    static curSearch() {
        var key = $('.selectpicker').selectpicker('val');
        var word = $('#curSearchInput').val();
        if (word == null || word == 'Search' || word == '' || key == null) {
            layer.tips('选择并填写搜索内容!', '#curSearchInput', {
                tips: 3
            });
        }
        else {
            window.location.href = CommonPage.param.url + '/search.do?key=' + key + '&word=' + word;
        }
    }

    static timeInit() {
        $('.datetimepicker').datetimepicker({
            format: 'yyyy-mm-dd hh:ii',
            language: 'zh-CN',//显示中文
            autoclose: true,//选中自动关闭
            todayBtn: true,//显示今日按钮
            todayHighlight:true,//高亮显示当前日期
        });
    }

    static bootBoxDel(url, par) {
        bootbox.confirm({
            title: "确认删除?",
            message: "你将无法恢复该文件！",
            size: "small",
            buttons: {
                confirm: {
                    label: '确定',
                    className: 'btn-danger'
                },
                cancel: {
                    label: '关闭',
                    className: 'btn-default'
                }
            },
            callback: function (result) {/* result is a boolean; true = OK, false = Cancel*/
                if (result) {
                    $.post(url, par, function (data) {
                        if (data.ret == 1) {
                            location.reload();
                        } else if (data.ret == 0) {
                            toastr.error(data.msg)
                        }
                    });
                }
            }
        })
    }
}